
import { request } from '../../request/index.js'
import { requestPayment, showToast, showModal } from '../../utils/asyncWx.js'
import regeneratorRuntime from '../../lib/runtime/runtime.js'


Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cartList: [],
    totalPrice: 0,
    totalNum: 0
  },
  goodsInfo: [],
  onShow() {
    const address = wx.getStorageSync("address")
    // 缓存中购物车数据
    let cartList = wx.getStorageSync("cart") || []
    cartList = cartList.filter(v => v.checked)
    // 判断是否选中
    this.setCart(cartList)
    this.setData({
      address
    })
  },
  // 修改购物车数据
  setCart(cartList) {
    let totalNum = 0
    let totalPrice = 0
    cartList.forEach(v => {
      totalNum += v.num
      totalPrice += v.goods_price * v.num
    })
    this.setData({
      totalNum,
      totalPrice,
      cartList
    })
  },
  // 支付功能
  async handleOrderPay() {
    try {
      const taken = wx.getStorageSync("taken")
      if (!taken) {
        wx.navigateTo({
          url: '/pages/auth/auth',
        })
        return
      }
      // 创建订单
      // 请求头参数
      // const header = { Authorization: taken }
      // 请求体参数
      const order_price = this.data.totalPrice
      const consignee_addr = this.data.address.all
      const goods = []
      let cartList = this.data.cartList
      cartList.forEach(v => goods.push({
        goods_id: v.goods_id,
        goods_number: v.num,
        goods_price: v.goods_price
      }))
      const orderParams = { order_price, consignee_addr, goods }
      // 发送请求，创建订单，获取订单号
      // let { order_number } = await request({ url: '/my/orders/create', methods: 'POST', data: orderParams })
      // 自己的假数据
      if (orderParams) {
        const order_number = "HMDD20190802000000000422"
        const pay = {
          timeStamp: "1564730510",
          nonceStr: "SReWbt3nEmpJo3tr",
          package: "prepay_id=wx02152148991420a3b39a90811023326800",
          signType: "MD5",
          paySign: "3A6943C3B865FA2B2C825CDCB33C5304"
        }
        const res = await showModal({ content: "是否确认支付？" })
        if (res.confirm) {
          await showToast({ title: "支付成功！" })
          // 支付成功修改购物车数据
          let newCart = wx.getStorageSync("cart");
          // 存入支付商品信息
          wx.setStorageSync("payGoods", newCart);
          newCart = newCart.filter(v => !v.checked)
          wx.setStorageSync("cart", newCart);
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/order/order',
            })
          },500)
        } else {
          await showToast({ title: "支付失败！" })
        }
      }
      // 发起预支付接口
      // let { pay } = await request({ url: '/my/orders/req_unifiedorder', methods: 'POST', data: order_number })
      // 发起微信支付
      // await requestPayment(pay)
      // 查询后台订单状态
      // let res = await request({ url: '/my/orders/req_unifiedorder', methods: 'POST', data: pay })
      // await showToast({ title: "支付成功！" })
      // wx.navigateTo({
      //   url: '/pages/order/order',
      // });
    } catch (err) {
      await showToast({title: "支付失败！"})
      console.log(err)
    }
  }
})